package mapper;

import bean.Dept;
import bean.Emp;
import org.apache.ibatis.annotations.*;

import java.util.List;

@CacheNamespace(blocking = true)
public interface EmpMapper {

    @Select("select * from emp where empstate=1")
    List<Emp> findAllEmp();

    @Insert("insert into emp values (null,#{ename},#{job},#{mgr},now(),#{sal},#{comm},#{deptNo},1)")
    void saveEmp(Emp emp);

    @Update("<script>update emp <set><if test='ename!=null'>ename=#{ename},</if><if test='sal!=null'>sal=#{sal},</if></set><where><if test='empNo!=null'>empno=#{empNo}</if></where></script>")
    void updateEmp(Emp emp);

    @Update("update emp set empstate=0 where empNo=#{id}")
    void deleteEmp(int id);


    @Select ("select * from emp where empno=#{id}")
    @Results(id="map1",value = {
            @Result(property = "empNo",column = "empno",id=true),//代表是主键
            @Result(property = "ename",column = "ename"),
            @Result(property = "dept",javaType = Dept.class,
                one=@One(select = "mapper.DeptMapper.findDeptByNo"),column = "deptno")
    })
    Emp findEmpByNo1(int id);

    @Select("select * from emp where empstate=1 and empno=#{id}")
    @Results(id = "map2",value={
            @Result(property = "empNo", column = "empno", id = true),
            @Result(property = "ename", column = "ename"),
            @Result(property = "hireDate1",column = "hiredate")
    })
    Emp find1(int id);
    @Select("select * from emp where empstate=1 and ename=#{ename}")
    @ResultMap("map2")
    Emp find2(String ename);
}
